/**
 * 初始化文档分类详情对话框
 */
var CategoryInfoDlg = {
    categoryInfoData: {},
    editor: null,
    validateFields: {
        category: {
            validators: {
                notEmpty: {
                    message: '文档分类名不能为空'
                }
            }
        }
    }
};

/**
 * 清除数据
 */
CategoryInfoDlg.clearData = function () {
    this.categoryInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
CategoryInfoDlg.set = function (key, val) {
    this.categoryInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
CategoryInfoDlg.get = function (key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
CategoryInfoDlg.close = function () {
    parent.layer.close(window.parent.Category.layerIndex);
}


/**
 * 点击分类ztree列表的选项时
 *
 * @param e
 * @param treeId
 * @param treeNode
 * @returns
 */
CategoryInfoDlg.onClickCategory = function(e, treeId, treeNode) {
    $("#pName").attr("value", CategoryInfoDlg.zTreeInstance.getSelectedVal());
    $("#parentId").attr("value", treeNode.id);
}
/**
 * 显示分类选择的树
 *
 * @returns
 */
CategoryInfoDlg.showCategorySelectTree = function() {
    var pName = $("#pName");
    var pNameOffset = $("#pName").offset();
    $("#parentCategoryMenu").css({
        left : pNameOffset.left + "px",
        top : pNameOffset.top + pName.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
}

/**
 * 隐藏分类选择的树
 */
CategoryInfoDlg.hideCategorySelectTree = function() {
    $("#parentCategoryMenu").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
}

/**
 * 收集数据
 */
CategoryInfoDlg.collectData = function() {
    this.set('id').set('categoryName').set('categoryDesc').set('parentId');
}
/**
 * 验证数据是否为空
 */
CategoryInfoDlg.validate = function () {
    $('#categoryForm').data("bootstrapValidator").resetForm();
    $('#categoryForm').bootstrapValidator('validate');
    return $("#categoryForm").data('bootstrapValidator').isValid();
};

/**
 * 提交添加
 */
CategoryInfoDlg.addSubmit = function () {

    this.clearData();
    this.collectData();

    if (!this.validate()) {
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/category/add", function (data) {
        Feng.success("添加成功!");
        window.parent.Category.table.refresh();
        CategoryInfoDlg.close();
    }, function (data) {
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.categoryInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
CategoryInfoDlg.editSubmit = function () {

    this.clearData();
    this.collectData();

    if (!this.validate()) {
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/category/update", function (data) {
        Feng.success("修改成功!");
        window.parent.Category.table.refresh();
        CategoryInfoDlg.close();
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.categoryInfoData);
    ajax.start();
}

function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "parentCategoryMenu" || $(
            event.target).parents("#parentCategoryMenu").length > 0)) {
        CategoryInfoDlg.hideCategorySelectTree();
    }
}

$(function () {
    Feng.initValidator("categoryForm", CategoryInfoDlg.validateFields);

    var ztree = new $ZTree("parentCategoryMenuTree", "/category/tree");
    ztree.bindOnClick(CategoryInfoDlg.onClickCategory);
    ztree.init();
    CategoryInfoDlg.zTreeInstance = ztree;
});
